package com.jxjx.www.hellodocker

import io.vertx.core.Vertx
import io.vertx.core.http.HttpMethod
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

fun main(args:Array<String>) {

    val vertx = Vertx.vertx()

    val router = Router.router(vertx)

    router.route("/hello/:hello").method(HttpMethod.GET).handler {
        CoroutineScope(Dispatchers.IO).launch {
            handleHello(it, it.request().getParam("hello"))
        }
    }

    router.route("/file/:file").method(HttpMethod.GET).handler { ctx ->
        vertx.fileSystem().readFile("/home/xujiajun/docker/${ctx.request().getParam("file")}") {
            if (it.succeeded()) {
                ctx.response().end(it.result())
            } else {
                ctx.response().end("file not found")
            }
        }
    }

    vertx.createHttpServer().requestHandler(router).listen(3000)
}

suspend fun handleHello(ctx: RoutingContext, hello: String) {
    ctx.response().end(hello)
}